﻿using System;
using NATTraversal;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Networking.Match;
using UnityEngine.Networking.Types;

// Token: 0x020000A9 RID: 169
[HelpURL("http://grabblesgame.com/nat-traversal/docs/class_n_a_t_traversal_1_1_network_manager.html")]
public class ExampleMigrationManager : MigrationManager
{
	// Token: 0x060003F2 RID: 1010 RVA: 0x00020891 File Offset: 0x0001EA91
	private void OnGUI()
	{
		if (base.hostWasShutdown)
		{
			this.OnGUIHost();
			return;
		}
		if (base.disconnectedFromHost && base.oldServerConnectionId != -1)
		{
			this.OnGUIClient();
		}
	}

	// Token: 0x060003F3 RID: 1011 RVA: 0x000208C4 File Offset: 0x0001EAC4
	private void OnGUIHost()
	{
		int num = 310;
		GUI.Label(new Rect(10f, (float)num, 200f, 40f), "Host Was Shutdown ID(" + base.oldServerConnectionId + ")");
		num += 25;
		if (Application.platform == RuntimePlatform.WebGLPlayer)
		{
			GUI.Label(new Rect(10f, (float)num, 200f, 40f), "Host Migration not supported for WebGL");
			return;
		}
		if (base.waitingReconnectToNewHost)
		{
			if (GUI.Button(new Rect(10f, (float)num, 200f, 20f), "Reconnect as Client"))
			{
				base.Reset(0);
				this.networkManager.networkAddress = base.newHostAddress;
				this.networkManager.StartClientAll(this.newHost.address, this.newHost.internalIP, this.newHost.port, this.newHost.guid, NetworkID.Invalid, this.newHost.externalIPv6, this.newHost.internalIPv6, null, string.Empty, 0, 0, false);
			}
			num += 25;
		}
		else
		{
			bool flag;
			if (GUI.Button(new Rect(10f, (float)num, 200f, 20f), "Pick New Host") && this.FindNewHost(out this.newHost, out flag))
			{
				base.newHostAddress = this.newHost.address;
				if (flag)
				{
					Debug.LogWarning("MigrationManager FindNewHost - new host is self?");
				}
				else
				{
					base.waitingReconnectToNewHost = true;
				}
			}
			num += 25;
		}
		if (GUI.Button(new Rect(10f, (float)num, 200f, 20f), "Leave Game"))
		{
			this.networkManager.SetupMigrationManager(null);
			this.networkManager.StopHost();
			base.Reset(-1);
		}
		num += 25;
	}

	// Token: 0x060003F4 RID: 1012 RVA: 0x00020A9C File Offset: 0x0001EC9C
	private void OnGUIClient()
	{
		int num = 300;
		GUI.Label(new Rect(10f, (float)num, 200f, 40f), "Lost Connection To Host ID(" + base.oldServerConnectionId + ")");
		num += 25;
		if (Application.platform == RuntimePlatform.WebGLPlayer)
		{
			GUI.Label(new Rect(10f, (float)num, 200f, 40f), "Host Migration not supported for WebGL");
			return;
		}
		if (base.waitingToBecomeNewHost)
		{
			GUI.Label(new Rect(10f, (float)num, 200f, 40f), "You are the new host");
			num += 25;
			if (GUI.Button(new Rect(10f, (float)num, 200f, 20f), "Start As Host"))
			{
				NetworkServer.Configure(this.networkManager.topo);
				string ipAddress = Network.player.ipAddress;
				string localIPv = this.networkManager.getLocalIPv6();
				string externalIP = this.networkManager.externalIP;
				string externalIPv = this.networkManager.externalIPv6;
				string text = string.Concat(new string[]
				{
					"SomeMatchName|",
					ipAddress,
					"|",
					externalIP,
					"|",
					localIPv,
					"|",
					externalIPv
				});
				if (this.networkManager.connectPunchthrough)
				{
					text = text + "|" + this.networkManager.natHelper.guid;
				}
				this.networkManager.matchMaker.CreateMatch(text, this.networkManager.matchSize, true, string.Empty, string.Empty, string.Empty, 0, 0, new NetworkMatch.DataResponseDelegate<MatchInfo>(this.networkManager.OnMatchCreate));
				this.BecomeNewHost(this.networkManager.networkPort);
			}
			num += 25;
		}
		else if (base.waitingReconnectToNewHost)
		{
			GUI.Label(new Rect(10f, (float)num, 200f, 40f), "New host is " + base.newHostAddress);
			num += 25;
			if (GUI.Button(new Rect(10f, (float)num, 200f, 20f), "Reconnect To New Host"))
			{
				this.ReconnectToNewHost();
			}
			num += 25;
		}
		else
		{
			bool flag;
			if (GUI.Button(new Rect(10f, (float)num, 200f, 20f), "Pick New Host") && this.FindNewHost(out this.newHost, out flag))
			{
				base.newHostAddress = this.newHost.address;
				if (flag)
				{
					base.waitingToBecomeNewHost = true;
				}
				else
				{
					base.waitingReconnectToNewHost = true;
				}
			}
			num += 25;
		}
		if (GUI.Button(new Rect(10f, (float)num, 200f, 20f), "Leave Game"))
		{
			this.networkManager.SetupMigrationManager(null);
			if (NetworkServer.active)
			{
				this.networkManager.StopHost();
			}
			else if (NetworkClient.active)
			{
				this.networkManager.StopClient();
			}
			base.Reset(-1);
		}
		num += 25;
	}
}
